<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="zh-CN">
<head>
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
	<meta charset="utf-8" />
	<title>基础模块</title>
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
	<link rel="stylesheet" href="<?php echo base_url('public/styles/bootstrap/css/bootstrap.min.css'); ?>" />
	<script src="<?php echo base_url('public/scripts/jquery-3.2.1.min.js'); ?>"></script>
</head>
<script>
	$(document).ready(function(){
		if( typeof(WebSocket) != "function" ) {
			$('body').html("<div id='container'><h1>发生错误</h1><p style='margin: 12px 15px 12px 15px;'>您的浏览器不支持 HTML5 Web Sockets，请尝试更换或升级</p></div>");
		}else{
			var websocket = new WebSocket('<?php echo config_item('websocket_server'); ?>');
			websocket.onopen = function (evt) {
				console.log("connected to WebSocket server.");
				websocket.send('hello');
			};
			websocket.onclose = function (evt) {
				console.log("server disconnected!");
			};
			websocket.onmessage = function (evt) {
				if(evt.data){
					var data = JSON.parse(evt.data);
					ajax_post({data:data},function(result){
						console.log(result);
						if(result.error){
							alert(result.msg);
						}else{
							$("#container").html(result.msg);
						}
					},'json');
				}
			};
			websocket.onerror = function (evt, e) {
				alert('websocket 服务器错误，请使用 console 工具进行调试');
				console.log(evt);
			};
		}
	});
	function ajax_post(data,success,type = 'text'){
		$.ajax({
			type: 'POST',
			url: '' ,
			data: data ,
			timeout: 5000,
			success: success,
			error: function(XMLHttpRequest,status,error){
				alert('数据返回有误！请刷新页面后重试');
			},
			complete : function(XMLHttpRequest,status){
				if(status == 'timeout'){
					ajax.abort();
					alert('操作超时！请重试');
				}
			},
			dataType: type
		});
	}
</script>
<div id="container"></div>
</body>
</html>